package com.chezai.client.utils;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.telephony.TelephonyManager;

import com.orhanobut.logger.Logger;

/**
 * 设置APN
 * Created by likunyang on 2017/8/9.
 */

public class ApnUtils {
    // APN列表资源
    protected static Uri APN_LIST_URI = Uri.parse("content://telephony/carriers");
    // 默认APN资源
    protected static Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
    private String[] UNI_APN = {"呱呱学车", "UNIM2M.NJM2MAPN", "460", "06", "default,supl"};
    private String TAG = this.getClass().getSimpleName();
    private Context mContext;
    private String NUMERIC = "";

    public ApnUtils(Context context) {
        this.mContext = context;
    }

    /**
     * 判断是否存在
     *
     * @return -1 未配置
     */
    public int isUNIApnExisted() {
        int apnId = -1;
        Cursor mCursor = mContext.getContentResolver().query(APN_LIST_URI, null,
                "apn =?", new String[]{"UNIM2M.NJM2MAPN"}, null);
        // 如果不存在CMNET，则添加。
        if (mCursor != null && mCursor.getCount() == 0) {
            Logger.t(TAG).d("APN 配置不存在。");
            addUniApn();
        }

        while (mCursor != null && mCursor.moveToNext()) {
            apnId = mCursor.getShort(mCursor.getColumnIndex("_id"));
            String name = mCursor.getString(mCursor.getColumnIndex("name"));
            String apn = mCursor.getString(mCursor.getColumnIndex("apn"));
            Integer current = mCursor.getInt(mCursor.getColumnIndex("current"));
            String type = mCursor.getString(mCursor.getColumnIndex("type"));
            Logger.t(TAG).d("APN" + name + " " + apn + " " + type + " " + current);
            if (("UNIM2M.NJM2MAPN".equals(apn))) {
                return apnId;
            } else {
                apnId = -1;
            }
        }
        if (mCursor != null) {
            mCursor.close();
        }
        return apnId;
    }

    /**
     * 根据apnId将设置的APN选中
     *
     * @param apnId
     * @return
     */
    public boolean setDefaultApn(int apnId) {
        boolean res = false;
        ContentResolver resolver = mContext.getContentResolver();
        ContentValues values = new ContentValues();
        values.put("apn_id", apnId);
        try {
            Cursor c = resolver.query(PREFERRED_APN_URI, new String[]{"name", "apn"}, "_id=" + apnId, null, null);
            if (c != null) {
                res = true;
                c.close();
            }else{
                resolver.update(PREFERRED_APN_URI, values, null, null);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return res;
        }
        return res;
    }

    /**
     * 得到SIM卡上的信息
     */
    private void getNumeric() {
        TelephonyManager iPhoneManager = (TelephonyManager) mContext
                .getSystemService(Context.TELEPHONY_SERVICE);
        NUMERIC = iPhoneManager.getSimOperator();
        Logger.t(TAG).d("NUMERIC =" + NUMERIC);
    }

    /**
     * 利用ContentProvider将添加的APN数据添加进入数据库
     *
     * @return
     */
    private int addUniApn() {
        Logger.t(TAG).d("开始添加 APN 设置");
        int apnId = -1;
        ContentResolver resolver = mContext.getContentResolver();
        ContentValues values = new ContentValues();

        values.put("name", UNI_APN[0]);
        values.put("apn", UNI_APN[1]);
        values.put("type", UNI_APN[4]);
        values.put("numeric", UNI_APN[2] + UNI_APN[3]);
        values.put("mcc", UNI_APN[2]);
        values.put("mnc", UNI_APN[3]);
        values.put("proxy", "");
        values.put("port", "");
        values.put("mmsproxy", "");
        values.put("mmsport", "");
        values.put("user", "");
        values.put("server", "");
        values.put("password", "");
        values.put("mmsc", "");
        values.put("current", 1);
        Cursor c = null;
        try {
            Uri newRow = resolver.insert(APN_LIST_URI, values);
            if (newRow != null) {
                c = resolver.query(newRow, null, null, null, null);
                int idindex = c.getColumnIndex("_id");
                c.moveToFirst();
                apnId = c.getShort(idindex);
                Logger.t(TAG).d("Robert New ID: " + apnId + ": Inserting new APN succeeded!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        if (c != null)
            c.close();
        return apnId;
    }
}
